let express = require('express')
let User = require('./crud.js') //9-30
let md5 = require('blueimp-md5')    //9-30
let Teacher = require('./teacher.js')
let Student = require('./student.js')
const teacher = require('./teacher.js')
// const { json } = require('body-parser')
let router = express.Router()

//增
router.post('/user/register', (req, res) => {
    // console.log(body, '查req.body的值');
    User.find({
        $or: [
            { username: body.username },
            { nickname: body.nickname }
        ]
    }, (err, data) => {
        if (err) {
            return res.status(500).json({
                code: 3000,
                flag: false,
                message: "查询失败，请联系后台"
            })
        }
        if (data.length !== 0) {
            console.log(1111);
            return res.status(200).json({
                code: 2000,
                flag: false,
                message: "用户昵称已存在"
            })
        }

        body.token = md5(md5(body.username) + "buka")

        new User(body).save().then((data, err) => {
            if (data) {
                return res.status(200).json({
                    code: 2000,
                    flag: true,
                    message: "数据存储成功"
                })

            }
            if (err) {
                return res.status(500).json({
                    code: 3000,
                    flag: false,
                    message: "存储失败，请联系后台"
                })
            }

        })
    })
})
//查
router.post("/user/login", (req, res) => {
    let body = req.body;
    // console.log(body, "login中的body")
    User.findOne({ username: body.username, password: body.password }, (err, data) => {
        if (err) {
            return res.status(500).json({
                code: 3000,
                flag: false,
                message: "登录失败，请联系后台"
            })
        }
        if (!data) {
            return res.status(200).json({
                code: 2000,
                flag: false,
                message: "用户名或密码不存在"
            })
        }
        return res.json({
            "code": 2000,
            "flag": true,
            "message": "登陆成功5201314",
            "data": {
                "token": data.token,
            }
        })

    })
})


//9-30 查看用户信息
router.get('/user/info', (req, res) => {
    let body = req.query;
    // console.log(body, 52525252);

    User.findOne({ token: body.token }, (err, data) => {
        if (err) {
            return res.status(500).json({
                code: 3000,
                flag: false,
                message: "token查询失败，请联系后台"
            })
        }
        if (!data) {
            return res.status(200).json({
                code: 2000,
                flag: false,
                message: "token不存在"
            })
        }
        return res.json({
            "code": 2000,
            "flag": true,
            "message": "成功拿到所有数据",
            "data": {
                "nicknane": data.nickname,
                "id": data._id
            }
        })

    })
})

//10-5 退出系统
router.post("/user/logout", (req, res) => {
    let body = req.body;
    // console.log(body, "退出系统");
    User.findOne({ token: body.token }, (err, data) => {
        if (err) {
            return res.status(500).json({
                code: 3000,
                flag: false,
                message: "退出失败，请联系后台"
            })
        }
        if (!data) {
            return res.status(200).json({
                code: 4000,
                flag: false,
                message: "token 不存在"
            })
        }
        return res.status(200).json({
            code: 2000,
            flag: true,
            message: "退出系统成功"
        })
    })
})

//教师查询 获取所有教师列表
router.get("/teacher/list", (req, res) => {
    console.log(req.body,"查询教师信息");
    Teacher.find({}, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                " flag": false,
                "message": "服务器错误"
            })
        }
        let content = data.length

        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "查询成功",
            "data": {
                content,
                "rows": data
            }
        })
    })
})


//获取教师列表，带分页功能
router.post("/teacher/list", (req, res) => {
    let page = req.body.page || 1;  //页数
    let size = req.body.size || 10; //一页显示多少个
    let searchMap = req.body.searchMap || {};
    // console.log(page, size, searchMap);
    let obj = {}

    searchMap.jobnumber ? obj["jobnumber"] = searchMap.jobnumber : obj;
    searchMap.name ? obj["name"] = searchMap.name : obj;
    searchMap.role ? obj["role"] = searchMap.role : obj;
    searchMap.entrydate ? obj["entrydate"] = searchMap.entrydate : obj;

    Teacher.find(obj, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                " flag": false,
                "message": "服务器错误"
            })
        }

        let content = data.length
        // console.log(obj,content);
        // skip 跳过多少条 limit 查询多少个  exec回调
        Teacher.find(obj).skip((page - 1) * parseInt(size)).limit(parseInt(size)).exec((err, data) => {
            if (err) {
                return res.status(500).json({
                    " code": 3000,
                    " flag": false,
                    "message": "服务器错误"
                })
            }
            return res.status(200).json({
                "code": 2000,
                "flag": true,
                "message": "查询成功",
                "data": {
                    content,
                    "rows": data
                }
            })
        })

    })
})

//新增教师功能(教师存储)
router.post("/teacher", (req, res) => {
    let obj = {  //先存储
        jobnumber: req.body.jobnumber,
        name: req.body.name,
        role: req.body.role,
        entrydate: req.body.entrydate,
        phone: req.body.phone,
    }
    new Teacher(obj).save(function (err) {
        if (err) {
            return res.status(500).json({
                "code": 3000,
                "flag": false,
                "message": "新增失败"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "新增教师数据成功",

        })
    })
})

//教师编辑
router.get("/teacher", (req, res) => {
    Teacher.findById(req.query.id, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                "flag": false,
                " message": "根据id查询教师失败"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "根据id查询成功",
            "data": data
        })
    })
})
//教师编辑更新操作
router.put("/teacher", (req, res) => {
    let id = req.body._id
    Teacher.findByIdAndUpdate(id, req.body, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                "flag": false,
                " message": "编辑更新出错"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "编辑更新成功",

        })
    })
})

//删除教师功能
router.delete('/teacher', (req, res) => {
    Teacher.findByIdAndRemove(req.body.id, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                "flag": false,
                " message": "删除教师失败"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "删除教师成功",

        })
    })
})

//获取学生列表，带分页功能
router.post("/student/list", (req, res) => {
    let page = req.body.page || 1;  //页数
    let size = req.body.size || 10; //一页显示多少个
    let searchMap = req.body.searchMap || {};
    // console.log(page, size, searchMap);
    let obj = {}
    searchMap.stunum ? obj["stunum"] = searchMap.stunum : obj;
    searchMap.name ? obj["name"] = searchMap.name : obj;
    searchMap.teacher ? obj["teacher"] = searchMap.teacher : obj;
    searchMap.class ? obj["class"] = searchMap.class : obj;

    Student.find(obj, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                " flag": false,
                "message": "服务器错误"
            })
        }

        let content = data.length
        // console.log(obj,content);
        // skip 跳过多少条 limit 查询多少个  exec回调
        Student.find(obj).skip((page - 1) * parseInt(size)).limit(parseInt(size)).exec((err, data) => {
            if (err) {
                return res.status(500).json({
                    " code": 3000,
                    " flag": false,
                    "message": "服务器错误"
                })
            }
            return res.status(200).json({
                "code": 2000,
                "flag": true,
                "message": "查询成功",
                "data": {
                    content,
                    "rows": data
                }
            })
        })

    })
})

//新增学生
router.post("/student", (req,res) => {
    let obj={
            stunum:req.body.stunum,
            name:req.body.name,
            admissiondate:req.body.admissiondate,
            phone:req.body.phone,
            teacher:req.body.teacher,
            class:req.body.class,
            job:req.body.job,
            money:req.body.money,
            }
      console.log(req.body,8888);
    new Student(obj).save(function (err) {
        if (err) {
            return res.status(500).json({
                "code": 3000,
                "flag": false,
                "message": "新增学生失败"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "新增学生数据成功",

        })
    })
}) 


//编辑
router.get("/student", (req, res) => {
    console.log(req.query);
   Student.findById(req.query.id, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                "flag": false,
                " message": "根据id查询教师失败"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "根据id查询成功",
            "data": data
        })
    })
})

//学生更新编辑
router.put("/student", (req, res) => {
    let id = req.body._id
    console.log(req.body);
    Student.findByIdAndUpdate(id, req.body, (err, data) => {
        if (err) {
            return res.status(500).json({
                " code": 3000,
                "flag": false,
                " message": "编辑更新出错"
            })
        }
        return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "编辑更新成功",

        })
    })
})

//删除学员
router.delete('/student',function (req,res) {
      Student.findByIdAndRemove(req.body.id,function (err) {
        if(err){
          return res.status(500).json({
            code: 3000,
            flag: false,
            message: "删除学员失败"
         })
       }
        return res.status(200).json({
          "code": 2000,
          "flag": true,
          "message": "删除学员成功"
       })
     })
    });

//校验密码是否正确
router.post("/user/pwd",(req,res)=>{
    let body=req.body
    console.log(body,"校证");
    User.findOne({
        _id:body.id,
        password:body.password
    },function(err,data){
         if(err){
                  return res.status(500).json({
            
                    code: 3000,
                    flag: false,
                    message: "server error"
                 })
               }
                if(!data){
                  return res.status(200).json({
                    code: 4000,
                    flag: false,
                    message: "密码不正确"
                 })
               }
                return res.json({
                  "code": 2000,
                  "flag": true,
                  "message": "密码正确"
               })
             
    })
})

//修改密码
router.put('/user/pwd',function (req,res) {
      console.log('put',req.body);
      var id = req.body.id;
      console.log(req.body);
      User.findOne({
        _id:id
     },function (err,data) {
        if(err){
          return res.status(500).json({
            code: 3000,
            message: "server error"
         })
       }
        if(!data){
          return res.status(200).json({
            code: 4000,
            flag: false,
            message: "密码不正确"
         })
       }
        data.password = req.body.password;
        User.findByIdAndUpdate(id,data,function (err) {
          if(err){
            return res.status(500).json({
              code: 3000,
              flag: false,
              message: "修改密码失败"
           })
         }
          return res.status(200).json({
            "code": 2000,
            "flag": true,
            "message": "修改密码成功"
         })
       })
     })
});
module.exports = router